﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
using org.pdfbox.pdfparser;
using org.pdfbox.pdmodel;
using org.pdfbox.util;

namespace TxM
{
    class XReader
    {
        string _sFilePath = string.Empty;
        TextFormat _sFormat = TextFormat.tfUndefined;
        string _sFileName = string.Empty;
        string _sText = string.Empty;

        public string Text
        {
            get { return _sText; }
            private set { _sText = value; }
        }

        public string FileName
        {
            get { return _sFileName; }
            private set { _sFileName = value; }
        }
	
        public string FilePath
	    {
    		get { return _sFilePath; }
	    	private set { _sFilePath = value; }
	    }

        public TextFormat Format
        {
            get { return _sFormat; }
	    	private set { _sFormat = value; }
        }

        public enum TextFormat
        {
            tfTxt,
            tfRtf,
            tfDoc,
            tfPdf,
            tfUndefined
        }

        public XReader(string sPath)
        {
            if (File.Exists(sPath))
            {
                FilePath = sPath;
                FileName = Path.GetFileNameWithoutExtension(sPath);
                FindExtension();
                Read();
            }
        }

        private void FindExtension()
        {
            string sExtension = Path.GetExtension(_sFilePath).ToLower();
            Format = TextFormat.tfUndefined;
            if (sExtension == ".txt")
                Format = TextFormat.tfTxt;
            else if (sExtension == ".rtf")
                Format = TextFormat.tfRtf;
            else if (sExtension == ".doc")
                Format = TextFormat.tfDoc;
            else if (sExtension == ".docx")
                Format = TextFormat.tfDoc;
            else if (sExtension == ".pdf")
                Format = TextFormat.tfPdf;
        }

        private void Read()
        {
            switch(Format)
            {
                case TextFormat.tfRtf:
                    ReadRtf();
                    break;
                case TextFormat.tfDoc:
                    ReadDoc();
                    break;
                case TextFormat.tfPdf:
                    ReadPdf();
                    break;
                case TextFormat.tfTxt:
                case TextFormat.tfUndefined:
                    ReadTxt();
                    break;
            }
        }

        private void ReadRtf()
        {
            RichTextBox oRichTextBox = new RichTextBox();
            oRichTextBox.LoadFile(FilePath);
            Text = oRichTextBox.Text;
        }

        private void ReadDoc()
        {
            Word.Application oWord = null;
            object oMissing = System.Type.Missing;
            Word.Document oDoc = null;
            try
            {
                oWord = new Word.Application();
                oDoc = new Word.Document();
                object oReadOnly = true;
                object oVisible = false;
                object oFileName = _sFilePath;
                oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing
                    , ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
                    , ref oMissing, ref oVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                Text = oDoc.Content.Text;
            }
            finally
            {
                if (oDoc != null)
                    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                if (oWord != null)
                    oWord.Application.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }

        private void ReadPdf()
        {
            PDDocument doc = PDDocument.load(FilePath);
            PDFTextStripper stripper = new PDFTextStripper();
            Text = stripper.getText(doc);
        }

        private void ReadTxt()
        {
            Text = File.ReadAllText(FilePath, Encoding.Default);
        }
    }
}
